package demo1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28779
 * Date: 2023-04-23
 * Time: 10:01
 */
public class BinaryTree {
    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.right = E;
        B.left = D;
        C.right = G;
        C.left = F;
        E.right = H;

        return A;
    }

    // 前序遍历
    public void preOrder(TreeNode root) {
        if(root == null) {
            return ;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }
    //中序遍历
    public void inOrder(TreeNode root) {
        if(root == null) {
            return ;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    //后续遍历
    public void postOrder(TreeNode root) {
        if(root == null) {
            return ;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }
    // 获取树中节点的个数
    public int size1(TreeNode root) {
        if(root == null) {
            return 0;
        }
        return size1(root.left) + size1(root.right) + 1;
    }

    public static int n;
    public int size2(TreeNode root) {
        if(root == null) {
            return 0;
        }
        n++;
        size2(root.left);
        size2(root.right);
        return n;
    }

    // 获取叶子节点的个数
    public static int usedSize = 0;
    public int getLeafNodeCount1(TreeNode root) {
        if(root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            return usedSize++;
        }
        getLeafNodeCount1(root.left);
        getLeafNodeCount1(root.right);
        return usedSize;
    }
    public int getLeafNodeCount2(TreeNode root) {
        if(root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
    }

    // 获取第K层节点的个数
    int getKLevelNodeCount(TreeNode root,int k) {
        if(root == null) {
            return 0;
        }
        if( k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left, k - 1) + getKLevelNodeCount(root.right, k - 1);
    }
    // 获取二叉树的高度
    public static int getHeight(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int l = getHeight(root.left);
        int r = getHeight(root.right);
        return (r > l ? r : l) + 1;
    }
    // 检测值为value的元素是否存在
    public TreeNode find(TreeNode root, char val) {
        if(root == null) {
            return null;
        }
        if(root.val == val) {
            return root;
        }
        TreeNode left = find(root.left, val);
        TreeNode right = find(root.right, val);
        return left == null ? right : left;
    }

    //判断两棵树是否相同
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p != null && q == null || p == null && q != null) {
            return false;
        }

        if(p == null || q == null) {
            return true;
        }

        if(p.val != q.val) {
            return false;
        }

        return isSameTree(p.left,q.left) && isSameTree(p.right, q.right);
    }

    //判断是否为另一颗树的子树
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if(root == null) {
            return false;
        }

        if( isSameTree(root, subRoot)) {
            return true;
        }

        if(isSubtree(root.left, subRoot)) {
            return true;
        }

        if(isSubtree(root.right, subRoot)) {
            return true;
        }

        return false;
    }

    //翻转二叉树
    public TreeNode invertTree(TreeNode root) {
        if(root == null) {
            return null;
        }
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;

        invertTree(root.left);
        invertTree(root.right);

        return root;
    }
    //判断平衡二叉树
    public boolean isBalanced(TreeNode root) {
        if(root == null) {
            return true;
        }
        int leftHigh = getHeight(root.left);
        int rightHigh = getHeight(root.right);

        //if(Math.abs(leftHigh - rightHigh)<= 1)
        return  Math.abs(leftHigh - rightHigh)<= 1 && isBalanced(root.left) && isBalanced(root.right);
    }

}